package ast;

import java.io.PrintStream;
import java.util.Scanner;
import java.util.Map;
import java.util.Stack;

public class UnaryMinusExpr extends Expr {
    public final Expr expr;

    public UnaryMinusExpr(Expr e, Location loc) {
        super(loc);
        expr = e;
    }

    public void print(PrintStream ps) {
        ps.print("-(");
        expr.print(ps);
        ps.print(")");
    }

    // interpret: return the negation of the value of the expression
    public AbstractValue evaluate (Stack<Map<String, AbstractValue>> state, Scanner scanner) throws Exception {
        AbstractValue value = expr.evaluate(state, scanner);
        /**
        // return according to the type of the value
        if (value instanceof Long) {
            return -value.longValue();
        } else {
            return -value.doubleValue();
        }
        */
        // if ZeroInt or ZeroFloat, return ZeroInt or ZeroFloat; if PosInt/PosFloat, return NegInt/NegFloat, and vice versa
        switch (value.getType()) {
            case ZeroFloat:
                return AbstractValue.ZeroFloat;
            case ZeroInt:
                return AbstractValue.ZeroInt;
            case PosFloat:
                return AbstractValue.NegFloat;
            case PosInt:
                return AbstractValue.NegInt;
            case AnyFloat:
                return AbstractValue.AnyFloat;
            case AnyInt:
                return AbstractValue.AnyInt;
            case NegFloat:
                return AbstractValue.PosFloat;
            case NegInt:
                return AbstractValue.PosInt;
            default: 
                return null;
        }
    }
}
